package chapter12;

import chapter11.Role;
import chapter12.mapper.RoleMapper;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.Random;

/**
 * Created with IntelliJ IDEA.
 * Description:
 *
 * @author lzy
 * @create 2018-04-16 下午5:18
 */
public class TestDemo2 {
    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("application.xml");
        SqlSessionTemplate template = ctx.getBean(SqlSessionTemplate.class);
        Role role = template.selectOne("RoleMapper.getRole", 1L);
        System.out.println(role.toString());

        RoleMapper mapper = ctx.getBean(RoleMapper.class);
        Role r2 = mapper.getRole(1L);
        System.out.println(r2.toString());

        Role r3 = new Role();
        r3.setRoleName("test2"+new Random().nextInt(100));
        r3.setNote("test-note-"+ new Random().nextInt(10000));
        int result = mapper.insertRole(r3);
        System.out.println("auto id:" + r3.getRoleId());
        System.out.println("insert into db success?" + (result > 0));
    }
}
